package com.ming.springcloud.consumer.controller;

import com.alibaba.fastjson.JSONObject;
import com.ming.springcloud.consumer.service.BookRibbonService;
import com.ming.springcloud.consumer.service.DeviceRibbonService;
import com.ming.springcloud.consumer.util.ResponseEntity;
import com.ming.springcloud.consumer.vo.Books;
import com.ming.springcloud.consumer.vo.Devices;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/device_ribbon")
public class DeviceRibbonController {
    @Resource
    private DeviceRibbonService deviceRibbonService;


    @RequestMapping(value = "/findAllDevices", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity findAllBook(){
        String userJSON = deviceRibbonService.findDevices();
        //   System.out.println(userJSON);
        return ResponseEntity.success().add("pageInfo",userJSON);
    }

    @RequestMapping(value = "/findDevicesById/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity findStudentsByIds(@PathVariable("id")Integer id){
        //    System.out.println("id="+id);
        String userJSON = deviceRibbonService.findDevicesById( id);
        System.out.println(userJSON);
        JSONObject object = (JSONObject) JSONObject.parse(userJSON);

        //获取回传的josn数据
        String deviceId=object.get("deviceId").toString();
        String deviceName=object.get("deviceName").toString();
        String deviceAddress=object.get("deviceAddress").toString();
        String deviceType=object.get("deviceType").toString();
        String remarks=object.get("remarks").toString();
        String serviceName=object.get("serviceName").toString();
        String ids=object.get("id").toString();

        Devices devices=new Devices();
        devices.setId(Integer.parseInt(ids));
        devices.setServiceName(serviceName);
        devices.setDeviceId(deviceId);
        devices.setDeviceName(deviceName);
        devices.setDeviceType(deviceType);
        devices.setDeviceAddress(deviceAddress);
        devices.setRemarks(remarks);

           System.out.println("学生信息="+devices);
        return  ResponseEntity.success().add("book",devices);
    }
}
